const Service = require('egg').Service;

class TemplateService extends Service {

    /**
     * 查找自己创建的模板
     *
     * @param {*} [params={}]
     * @returns
     * @memberof TemplateService
     */
    async findTemplate(params = {}) {
        const pageSize = params && parseInt(params.pageSize) || 10; // 使用分页
        const page = params && parseInt(params.page) || 1;
        delete params.pageSize;
        delete params.page;
        params.author = this.ctx.state.user.id
        return new Promise((resolve, reject) => {
            this.ctx.model.Template.find(params).limit(pageSize).skip(pageSize * (page - 1)).sort('-createDate').lean().exec((err, docs) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(docs);
                }
            })
        })
    }

    /**
     * 获取共用列表
     *
     * @param {*} [params={}]
     * @returns
     * @memberof TemplateService
     */
    async findPublicTemplate(params = {}) {
        const pageSize = params && parseInt(params.pageSize) || 10; // 使用分页
        const page = params && parseInt(params.page) || 1;
        delete params.pageSize;
        delete params.page;
        return this.ctx.model.Template.find(params, 'imgUrl description author createDate visitCounts -target').limit(pageSize).skip(pageSize * (page - 1)).sort('-createDate').lean();
    }

    /**
     * 查询模板详情并更新查看数
     *
     * @param {*} id
     * @returns
     * @memberof TemplateService
     */
    async findTemplateDetail(id) {
        // You should set the new option to true to return the document after update was applied.
        return this.ctx.model.Template.findOneAndUpdate({ _id: id }, { $inc: { visitCounts: 1 } }, { new: true }).lean()
    }


    async findNextTemplateDetail(id) {
        //Get next post you can like this:
        // db.posts.find({_id: {$gt: curId}}).sort({_id: 1 }).limit(1)

        // Get previous post you can like this:
        // db.posts.find({_id: {$lt: curId}}).sort({_id: -1 }).limit(1)

        // Find previous item:
        // Post.findOne({_id: {$lt: curId}}).sort({_id: -1}).exec(cb)

        // Find next item:
        // Post.findOne({_id: {$gt: curId}}).sort({_id: 1}).exec(cb)
        return this.ctx.model.Template.findOne({ _id: { $lt: id } }).sort({ _id: -1 }).lean()
    }


    /**
     * 获取模板详情
     * @param {} params
     */
    async findOneTemplate(id) {
        return new Promise(async (resolve, reject) => {
            this.ctx.model.Template.findOne({
                _id: id
            }).lean()
                .exec((err, doc) => {
                    if (err) {
                        console.log(err);
                        reject(err);
                    } else {
                        resolve(doc);
                    }
                });
        })
    }

    async createTemplate(data) {
        return new Promise(async (resolve, reject) => {
            // this.ctx得到的是请求的上下文，在里面可以直接访问到了Todo modal
            this.ctx.model.Template({
                ...data
            }).save((err, data) => { //  这里调用的是mongoose的save方法创建一条新的记录
                if (err) {
                    reject(err);
                    return
                }
                resolve(data)
            });
        })
    }
    async deleteTemplate(id) {
        const author = this.ctx.state.user.id
        return new Promise(async (resolve, reject) => {
            this.ctx.model.Template.findOneAndRemove({
                author,
                _id: id
            }).lean()
                .exec((err, doc) => {
                    if (err) {
                        console.log(err);
                        reject(err);
                    } else {
                        resolve(doc);
                    }
                });
        })
    }
}
module.exports = TemplateService;